home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / LVSZ82 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.5 KB  |  182 lines

  1. package java.awt;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class GridLayout implements LayoutManager, Serializable {
  6.    int hgap;
  7.    int vgap;
  8.    int rows;
  9.    int cols;
  10.  
  11.    public GridLayout() {
  12.       this(1, 0, 0, 0);
  13.    }
  14.  
  15.    public GridLayout(int rows, int cols) {
  16.       this(rows, cols, 0, 0);
  17.    }
  18.  
  19.    public GridLayout(int rows, int cols, int hgap, int vgap) {
  20.       if (rows == 0 && cols == 0) {
  21.          throw new IllegalArgumentException("rows and cols cannot both be zero");
  22.       } else {
  23.          this.rows = rows;
  24.          this.cols = cols;
  25.          this.hgap = hgap;
  26.          this.vgap = vgap;
  27.       }
  28.    }
  29.  
  30.    public void addLayoutComponent(String name, Component comp) {
  31.    }
  32.  
  33.    public int getColumns() {
  34.       return this.cols;
  35.    }
  36.  
  37.    public int getHgap() {
  38.       return this.hgap;
  39.    }
  40.  
  41.    public int getRows() {
  42.       return this.rows;
  43.    }
  44.  
  45.    public int getVgap() {
  46.       return this.vgap;
  47.    }
  48.  
  49.    public void layoutContainer(Container parent) {
  50.       synchronized(((Component)parent).getTreeLock()) {
  51.          Insets insets = parent.getInsets();
  52.          int ncomponents = parent.getComponentCount();
  53.          int nrows = this.rows;
  54.          int ncols = this.cols;
  55.          if (ncomponents == 0) {
  56.             return;
  57.          }
  58.  
  59.          if (nrows > 0) {
  60.             ncols = (ncomponents + nrows - 1) / nrows;
  61.          } else {
  62.             nrows = (ncomponents + ncols - 1) / ncols;
  63.          }
  64.  
  65.          int w = parent.width - (insets.left + insets.right);
  66.          int h = parent.height - (insets.top + insets.bottom);
  67.          w = (w - (ncols - 1) * this.hgap) / ncols;
  68.          h = (h - (nrows - 1) * this.vgap) / nrows;
  69.          int c = 0;
  70.  
  71.          for(int x = insets.left; c < ncols; x += w + this.hgap) {
  72.             int r = 0;
  73.  
  74.             for(int y = insets.top; r < nrows; y += h + this.vgap) {
  75.                int i = r * ncols + c;
  76.                if (i < ncomponents) {
  77.                   parent.getComponent(i).setBounds(x, y, w, h);
  78.                }
  79.  
  80.                ++r;
  81.             }
  82.  
  83.             ++c;
  84.          }
  85.       }
  86.  
  87.    }
  88.  
  89.    public Dimension minimumLayoutSize(Container parent) {
  90.       synchronized(((Component)parent).getTreeLock()) {
  91.          Insets insets = parent.getInsets();
  92.          int ncomponents = parent.getComponentCount();
  93.          int nrows = this.rows;
  94.          int ncols = this.cols;
  95.          if (nrows > 0) {
  96.             ncols = (ncomponents + nrows - 1) / nrows;
  97.          } else {
  98.             nrows = (ncomponents + ncols - 1) / ncols;
  99.          }
  100.  
  101.          int w = 0;
  102.          int h = 0;
  103.  
  104.          for(int i = 0; i < ncomponents; ++i) {
  105.             Component comp = parent.getComponent(i);
  106.             Dimension d = comp.getMinimumSize();
  107.             if (w < d.width) {
  108.                w = d.width;
  109.             }
  110.  
  111.             if (h < d.height) {
  112.                h = d.height;
  113.             }
  114.          }
  115.  
  116.          return new Dimension(insets.left + insets.right + ncols * w + (ncols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (nrows - 1) * this.vgap);
  117.       }
  118.    }
  119.  
  120.    public Dimension preferredLayoutSize(Container parent) {
  121.       synchronized(((Component)parent).getTreeLock()) {
  122.          Insets insets = parent.getInsets();
  123.          int ncomponents = parent.getComponentCount();
  124.          int nrows = this.rows;
  125.          int ncols = this.cols;
  126.          if (nrows > 0) {
  127.             ncols = (ncomponents + nrows - 1) / nrows;
  128.          } else {
  129.             nrows = (ncomponents + ncols - 1) / ncols;
  130.          }
  131.  
  132.          int w = 0;
  133.          int h = 0;
  134.  
  135.          for(int i = 0; i < ncomponents; ++i) {
  136.             Component comp = parent.getComponent(i);
  137.             Dimension d = comp.getPreferredSize();
  138.             if (w < d.width) {
  139.                w = d.width;
  140.             }
  141.  
  142.             if (h < d.height) {
  143.                h = d.height;
  144.             }
  145.          }
  146.  
  147.          return new Dimension(insets.left + insets.right + ncols * w + (ncols - 1) * this.hgap, insets.top + insets.bottom + nrows * h + (nrows - 1) * this.vgap);
  148.       }
  149.    }
  150.  
  151.    public void removeLayoutComponent(Component comp) {
  152.    }
  153.  
  154.    public void setColumns(int cols) {
  155.       if (cols == 0 && this.rows == 0) {
  156.          throw new IllegalArgumentException("rows and cols cannot both be zero");
  157.       } else {
  158.          this.cols = cols;
  159.       }
  160.    }
  161.  
  162.    public void setHgap(int hgap) {
  163.       this.hgap = hgap;
  164.    }
  165.  
  166.    public void setRows(int rows) {
  167.       if (rows == 0 && this.cols == 0) {
  168.          throw new IllegalArgumentException("rows and cols cannot both be zero");
  169.       } else {
  170.          this.rows = rows;
  171.       }
  172.    }
  173.  
  174.    public void setVgap(int vgap) {
  175.       this.vgap = vgap;
  176.    }
  177.  
  178.    public String toString() {
  179.       return this.getClass().getName() + "[hgap=" + this.hgap + ",vgap=" + this.vgap + ",rows=" + this.rows + ",cols=" + this.cols + "]";
  180.    }
  181. }
  182.